package v1

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"mycms/global"
	"mycms/model/common/response"
	systemReq "mycms/model/system/request"
	systemRes "mycms/model/system/response"
	"mycms/service/admin"
	"mycms/service/common"
)


// @Tags 后台接口
// @Summary 角色列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param id formData string true "菜单ID"
// @Success 200 {object} systemRes.AdminLoginResponse
// @Router /admin/v1/roleList [get]
// @auth      xueneng
func (b *BaseApi)GetRoleList(c *gin.Context)  {
	var param systemReq.SearchRoleParams

	if ok := c.Bind(&param); ok != nil{
		//	返回错误信息
		response.FailWithMessage(ok.Error(), c)
		return
	}

	err, list, total := admin.GetRoleList(param)

	if err != nil{
		global.LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	}else{
		response.OkWithDetailed(response.PageResult{
			List:   list,
			Total:  total,
			Page:   param.Page,
			Limit:  param.Limit,
		}, "查询成功", c)
	}
}

// @Tags 后台接口
// @Summary 新增角色
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param id formData string true "菜单ID"
// @Success 200 {object} systemRes.AdminLoginResponse
// @Router /admin/v1/roleCreate [post]
// @auth      xueneng
func (b *BaseApi)CreateRole (c *gin.Context) {
	var param systemReq.CreateRoleParams

	err := c.BindJSON(&param)

	if err != nil{
		//	返回错误信息
		response.FailWithMessage(err.Error(), c)
		return
	}

	//	验证参数是否合法
	if valErr := common.Validate.Struct(param); valErr != nil{
		response.FailWithMessage(common.Translate(valErr, param), c)
		return
	}

	errs := admin.CreateRole(param)

	if errs != nil {
		global.LOG.Error("新增失败!", zap.Error(errs))
		response.FailWithDetailed("", "新增失败："+errs.Error(), c)
	} else {
		response.OkWithDetailed("", "新增成功", c)
	}
}

// @Tags 后台接口
// @Summary 角色详情
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param id formData string true "角色ID"
// @Success 200 {object} systemRes.AdminLoginResponse
// @Router /admin/v1/roleCreate [post]
// @auth      xueneng
func (b *BaseApi)GetRoleInfo (c *gin.Context) {
	var param systemReq.RoleInfoParams

	if ok := c.Bind(&param); ok != nil{
		//	返回错误信息
		response.FailWithMessage(ok.Error(), c)
		return
	}

	errs,res := admin.GetRoleInfo(param)

	if errs != nil {
		global.LOG.Error("获取失败!", zap.Error(errs))
		response.FailWithMessage("获取失败！", c)
	} else {
		response.OkWithDetailed(systemRes.RoleInfoResponse{
			Id:     res.ID,
			Name:   res.Name,
			Code:   res.Code,
			Status: res.Status,
			Remark: res.Remark,
			RoleMenu: res.RoleMenu,
		}, "查询成功", c)
	}
}


// @Tags 后台接口
// @Summary 编辑角色
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param id formData string true "角色ID"
// @Success 200 {object} systemRes.AdminLoginResponse
// @Router /admin/v1/roleCreate [post]
// @auth      xueneng
func (b *BaseApi)UpdateRole (c *gin.Context) {
	var param systemReq.UpdateRoleParams
	err := c.BindJSON(&param)

	if err != nil{
		//	返回错误信息
		response.FailWithMessage(err.Error(), c)
		return
	}

	//	验证参数是否合法
	if valErr := common.Validate.Struct(param); valErr != nil{
		response.FailWithMessage(common.Translate(valErr, param), c)
		return
	}

	errs := admin.UpdateRole(param)

	if errs != nil {
		global.LOG.Error("保存失败!", zap.Error(errs))
		response.FailWithMessage( "保存失败！", c)
	} else {
		response.OkWithDetailed("", "保存成功", c)
	}
}


// @Tags 后台接口
// @Summary 编辑角色
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param id formData string true "角色ID"
// @Success 200 {object} systemRes.AdminLoginResponse
// @Router /admin/v1/roleDel [post]
// @auth      xueneng
func (b *BaseApi)DelRole (c *gin.Context) {
	var param systemReq.RoleDelParams

	err := c.Bind(&param)

	if err != nil{
		//	返回错误信息
		response.FailWithMessage(err.Error(), c)
		return
	}

	//	验证参数是否合法
	if valErr := common.Validate.Struct(param); valErr != nil{
		response.FailWithMessage(common.Translate(valErr, param), c)
		return
	}

	errs := admin.DelRole(param)

	if errs != nil {
		global.LOG.Error("删除失败!", zap.Error(errs))
		response.FailWithDetailed("", "删除失败："+errs.Error(), c)
	} else {
		response.OkWithDetailed("", "删除成功", c)
	}
}
